package com.ycp.erp;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
import com.one.common.CommonApp;
import com.one.common.config.CMemoryData;
import com.one.common.config.MyContact;
import com.one.common.receiver.ActivityLifecycleListener;
import com.one.common.view.pagestate.refreshlayout.TwinklingRefreshLayout;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;
import static com.one.common.config.MyContact.BUGLY_APPID_CAR;
import static com.one.common.config.MyContact.UM_CAR;
import static com.one.common.config.MyContact.UPLOAD_LOCATION_TIME;


/**
 * creator: zhulunjun
 * time:    2018/8/3
 * describe: 入口
 */
public class App extends MultiDexApplication {

    private static Application sApp;

    public static Application getInstance() {
        return sApp;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sApp = this;
        CommonApp.init(this);
        CMemoryData.setAppSource(MyContact.CAR);
        CrashReport.initCrashReport(sApp, BUGLY_APPID_CAR, true);

        // 注册activity生命周期的回调
        registerActivityLifecycleCallbacks(new ActivityLifecycleListener());

        // 友盟
        UMConfigure.init(sApp, UM_CAR, "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");

//        PlatformConfig.setWeixin(WX_APP_KEY_CAR, WX_APP_SECRET_CAR);

//        // 初始钱包
//        WalletUtils.initWallet(CommonApp.config);
//
//        CommonApp.setWalletListener(activity ->
//                WalletUtils.launchWalletBill(activity));

        setAlarm();
        setPower();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    public void setAlarm() {
        //创建Alarm并启动
        Intent intent = new Intent("LOCATION_CLOCK");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        // 每五秒唤醒一次
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), UPLOAD_LOCATION_TIME, pendingIntent);
    }

    public void setPower() {
        //针对熄屏后cpu休眠导致的无法联网、定位失败问题,通过定期点亮屏幕实现联网,本操作会导致cpu无法休眠耗电量增加,谨慎使用
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = powerManager
                .newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
        wl.acquire(UPLOAD_LOCATION_TIME /*10 minutes*/);
        //点亮屏幕
        wl.release();
        //释放
    }


}
